package com.yunshipei.ui.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yunshipei.enterplorer.R;
import com.yunshipei.model.MainTab;


public class TabBar extends LinearLayout implements View.OnClickListener {

    private static final int index = (3 << 24);

    private OnTabChangeListener mOnTabChangedListener;

    private int width;
    private int height;

    private TextView mNumView;

    public TabBar(Context context) {
        super(context);
        initView(context);
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TabBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setOrientation(HORIZONTAL);
    }

    public void initMainTab(Context context, MainTab[] mainTabs) {
        for (int i = 0; i < mainTabs.length; i++) {
            View itemView = View.inflate(context, R.layout.tab_bar_item, null);
            TextView itemIcon = (TextView) itemView.findViewById(R.id.tab_bar_btn);
            TextView itemNum = (TextView) itemView.findViewById(R.id.tab_bar_de_num);
            itemNum.setVisibility(GONE);
            if (mainTabs[i].showNum) {
                mNumView = itemNum;
            }
            itemIcon.setText(mainTabs[i].name);
            Drawable top = getResources().getDrawable(mainTabs[i].resIconSelector);
            itemIcon.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
            itemView.setOnClickListener(this);
            itemView.setTag(index, i);
            addView(itemView);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = this.getLayoutParams().width;
        height = this.getLayoutParams().height;
        if (width == -1) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        if (height == -1) {
            height = MeasureSpec.getSize(heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int childwidth = width / getChildCount();
        int childheight = height;
        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);
            childAt.layout(i * childwidth, 0, (i + 1) * childwidth, childheight);
        }
    }

    public void setOnTabChangeListener(OnTabChangeListener listener) {
        mOnTabChangedListener = listener;
    }

    public void unRegistListener() {
        mOnTabChangedListener = null;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag(index);
        setCurrentTab(position);
    }

    public interface OnTabChangeListener {
        void onTabChange(int position);
    }

    public void setCurrentTab(int index) {
        for (int i = 0; i < getChildCount(); i++) {
            ViewGroup child = (ViewGroup) getChildAt(i);
            View viewById = child.findViewById(R.id.tab_bar_btn);
            if (index != i) {
                viewById.setSelected(false);
            } else {
                viewById.setSelected(true);
            }

        }
        if (mOnTabChangedListener != null) {
            mOnTabChangedListener.onTabChange(index);
        }
    }

    public void setMessageCount(int count) {
        if (mNumView != null) {
            if (count == 0) {
                mNumView.setText(String.valueOf(0));
                mNumView.setVisibility(View.GONE);
            } else if (count > 0 && count < 100) {
                mNumView.setVisibility(View.VISIBLE);
                mNumView.setText(String.valueOf(count));
            } else {
                mNumView.setVisibility(View.VISIBLE);
                mNumView.setText(R.string.no_read_message);
            }
        }
    }
}
